package com.paireach.scmc.http;

import com.blankj.utilcode.util.DeviceUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.paireach.scmc.R;
import com.paireach.scmc.constant.StringConstant;
import com.paireach.scmc.utils.CommonUtil;
import com.paireach.scmc.utils.GsonUtil;
import com.paireach.scmc.utils.base64.CryptoUtil;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ${cqc} on 2017/8/10.
 */

public class HttpClientUtil {

    private static final String BASE_URL = NetUrl.BASE_URL;
//    private static final String BASE_URL = "http://106.14.39.65:8285/itip/client/";

    private static AsyncHttpClient client = new AsyncHttpClient();//异步
    private static String sessionId = null;
    private static PersistentCookieStore cookieStore;

    static {
        client.setTimeout(10000);
        client.setMaxRetriesAndTimeout(2, 1000);//请求失败后，默认会再请求5次
    }

    /**
     * 无参
     */
    public static void getBytes(String url, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), responseHandler);
    }

    public static void postBytes(String url, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), responseHandler);
    }

    public static void getJson(String url, JsonHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), responseHandler);
    }

    public static void postJson(String url, JsonHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), responseHandler);
    }

    /**
     * 有参
     */
    // AsyncHttpResponseHandler
    public static void getBytes(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void postBytes(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    //JsonHttpResponseHandler
    public static void getJson(String url, RequestParams params, JsonHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    /**
     * map转json
     *
     * @param module
     * @param operation
     * @param map
     * @param responseHandler
     */
    public static void postJson(String module, String operation, Map<String, String> map, JsonHttpResponseHandler responseHandler) {
        Map<String, Object> paramMap = new HashMap<>();

        paramMap.put("requestEntity", map);//动态参数
        paramMap.put("module", module);
        paramMap.put("operation", operation);
        paramMap.put("appversion", CommonUtil.getString(R.string.app_version));
        paramMap.put("apptype", "android");
        paramMap.put("appsystem", DeviceUtils.getSDKVersion() + "");

        String param = GsonUtil.mapToJson(paramMap);

        RequestParams params = new RequestParams();
        params.put("sign", CryptoUtil.digestByMD5(param + StringConstant.API_KEY));
        params.put("parameter", param);
        client.post(BASE_URL, params, responseHandler);
    }


    public static void postJson3(String url, RequestParams params, JsonHttpResponseHandler responseHandler) {
        params.put(",", "");
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }


    /**
     * 清空所有的请求
     */
    public static void cancelAllRequests() {
        client.cancelAllRequests(true);
    }

    /**
     * sessionId
     */
    public static String getSessionId() {
        return sessionId;
    }

    public static void setSessionId(String sessionId) {
        HttpClientUtil.sessionId = sessionId;
    }

    /**
     * cookieStore
     */
    public static PersistentCookieStore getCookieStore() {
        return cookieStore;
    }

    public static void setCookieStore(PersistentCookieStore cookieStore) {
        HttpClientUtil.cookieStore = cookieStore;
        client.setCookieStore(cookieStore);
    }




    /**
     * map转object再转json
     *
     * @param module
     * @param operation
     * @param map
     * @param responseHandler
     */
    public static void postJson2(String module, String operation, Map<String, String> map, JsonHttpResponseHandler responseHandler) {
        try {
            JSONObject parameterObj = new JSONObject();

            parameterObj.put("module", module);
            parameterObj.put("operation", operation);
            parameterObj.put("appversion", CommonUtil.getString(R.string.app_version));
            parameterObj.put("apptype", "android");
            parameterObj.put("appsystem", DeviceUtils.getSDKVersion() + "");
            JSONObject requestEntity = new JSONObject(map);

            parameterObj.put("requestEntity", requestEntity);


            RequestParams params = new RequestParams();
            params.put("sign", CryptoUtil.digestByMD5(parameterObj.toString() + StringConstant.API_KEY));
            params.put("parameter", parameterObj.toString());
            client.post(BASE_URL, params, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void postJson(String module, String operation, Map<String, String> map, TextHttpResponseHandler responseHandler) {
        Map<String, Object> paramMap = new HashMap<>();

        paramMap.put("requestEntity", map);//动态参数
        paramMap.put("module", module);
        paramMap.put("operation", operation);
        paramMap.put("appversion", CommonUtil.getString(R.string.app_version));
        paramMap.put("apptype", "android");
        paramMap.put("appsystem", DeviceUtils.getSDKVersion() + "");

        String param = GsonUtil.mapToJson(paramMap);

        RequestParams params = new RequestParams();
        params.put("sign", CryptoUtil.digestByMD5(param + StringConstant.API_KEY));
        params.put("parameter", param);
        client.post(BASE_URL, params, responseHandler);
    }

}
